<?PHP
/**
 * LianPHP调试类库
 * 
 * @author 大春 dachun.net
 * @version 1.0  
 * @copyright Copyright &copy; 2010 lian wan chun
 */

class DeBug extends LianPhp {
	/**
	 * 调试函数.
	 *
	 * @param unknown_type $variable 需要调试打印内容
	 * @param bool $echo  是否输出调试信息
	 * @param bool $exit  是否终止
	 * @return unknown
	 */
	public function printDebug($variable = "", $echo = true, $exit = true){
		ob_start();
		echo "<div style=\"font-size: 12pt;background-color: #EEEEEE;text-align: left;width:100%\">调试<br>\n";
		/*
		$endTime = microtime(TRUE);
		$total_run_time	 = 	number_format(($endTime - $GLOBALS['g_BeginTime']), 3);
		echo "执行时间:".$total_run_time."s<br>";
		$startMem    =  array_sum(explode(' ', $GLOBALS['g_StartUseMems']));
		$endMem     =  array_sum(explode(' ', memory_get_usage()));
		echo "内存消耗:". number_format(($endMem - $startMem)/1024).' kb<br>';
		*/
		echo "当前页面:".$_SERVER['PHP_SELF']."<br>\n";
		echo "请求方法:".$_SERVER['REQUEST_METHOD']."<br>\n";
		echo "通信协议:".$_SERVER['SERVER_PROTOCOL']."<br>\n";
		echo "用户代理:".$_SERVER['HTTP_USER_AGENT']."<br>\n";
		echo "输出编码:".$_SERVER['REQUEST_METHOD']."<br>\n";
		if (isset($_POST)){
			echo "POST值:";
			foreach ($_POST as $key => $val){
				echo $key." = '".$val."',";
			}
			echo "<br>\n";
		}
		if (isset($_GET)){
			echo "GET值:";
			foreach ($_GET as $key => $val){
				echo $key." = '".$val."',";
			}
		}
		LianPhp::core()->Safe->dump($variable);
		echo "<br></div>\n";
		$output = ob_get_clean();
		if ($echo) echo $output;
		if ($exit) exit;
		return $output;
	}

	/**
	 * 区间调试开始
	 *
	 * @param string $label		调试标记
	 */
	public function debug_start($label=''){
		$GLOBALS[$label]['_beginTime'] = microtime(TRUE);
		if ( MEMORY_LIMIT_ON )  $GLOBALS[$label]['_beginMem'] = memory_get_usage();
	}

	/**
	 * 区间调试结束
	 *
	 * @param string $label		调试标记
	 * @example 
	 * debug_start("a1");
	 * echo "hello word";
	 * debug_end("a1");
	 */
	public function debug_end($label=''){
		$GLOBALS[$label]['_endTime'] = microtime(TRUE);
		echo '<div style="font-size: 12pt;background-color: #EEEEEE;text-align: center;width:100%">Process '.$label.': Times '.number_format($GLOBALS[$label]['_endTime']-$GLOBALS[$label]['_beginTime'],6).'s ';
		if ( MEMORY_LIMIT_ON )  {
			$GLOBALS[$label]['_endMem'] = memory_get_usage();
			echo ' Memories '.number_format(($GLOBALS[$label]['_endMem']-$GLOBALS[$label]['_beginMem'])/1024).' k';
		}
		echo '</div>';
	}
}
?>